/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cashdevice_status.h"
#include <string.h>
#include <string>
#include <map>
#include "cashdevice_def.h"

#define DEFINE_ERR(id, err) {id, err}

namespace SwanLink {
namespace Finance {
    static std::map<int, const std::string> g_sErr = {
        DEFINE_ERR(SUCCESS,                   "成功"),
        DEFINE_ERR(ERROR,                    "失败"),
        DEFINE_ERR(CANCEL,                    "取消"),
        DEFINE_ERR(TIMEOUT,                   "超时"),
        DEFINE_ERR(HANDLE_INVALID,            "句柄无效"),
        DEFINE_ERR(NOT_INIT,                  "未初始化"),

        DEFINE_ERR(ERR_DEVICE_NOT_OPEN,        "设备未打开"),
        DEFINE_ERR(ERR_DEVICE_NOT_SUPPORT,     "设备不支持"),
        DEFINE_ERR(ERR_DEVICE_OPEN_ALREADY,    "设备已打开"),
        DEFINE_ERR(ERR_DEVICE_BUSY,            "设备忙"),
        DEFINE_ERR(ERR_DEVICE_NOT_EXIST,       "设备不存在"),
        DEFINE_ERR(ERR_DEVICE_OPEN_FAILED,     "设备打开失败"),
        DEFINE_ERR(ERR_DEVICE_PERMISSION_DENY, "权限问题"),
        DEFINE_ERR(ERR_DEVICE_NO_DEVBUS,       "没有设备节点"),
        DEFINE_ERR(ERR_DEVICE_DRIVER_ERROR,    "驱动内核错误"),
        DEFINE_ERR(ERR_DEVICE_DISCONNECT,      "设备断开连接"),

        DEFINE_ERR(ERR_PLUGIN_UNLOADED,        "插件未加载"),
        DEFINE_ERR(ERR_PLUGIN_NOT_EXIST,       "插件不存在"),
        DEFINE_ERR(ERR_PLUGIN_LOAD_FAILED,     "插件加载失败"),
        DEFINE_ERR(ERR_PLUGIN_PATH_UNFIND,     "插件路径错误"),
        DEFINE_ERR(ERR_TRANS_NOT_SUPPORT,      "通讯方式不支持"),
        DEFINE_ERR(ERR_TRANS_PORT_TACKUP,      "通信端口被占用"),
        DEFINE_ERR(ERR_TRANS_PARAM_ERROR,      "通信参数错误"),
        DEFINE_ERR(ERR_TRANS_PROC_ERROR,       "通信过程异常"),
        DEFINE_ERR(ERR_TRANS_PROTO_ERROR,      "通信协议错误"),
        DEFINE_ERR(ERR_TRANS_SEND_FAILED,      "发送失败"),
        DEFINE_ERR(ERR_TRANS_SEND_UNFINISHED,  "发送未完成"),
        DEFINE_ERR(ERR_TRANS_RECV_FAILED,      "接收失败"),
        DEFINE_ERR(ERR_TRANS_RECV_UNFINISHED,  "接收未完成"),

        DEFINE_ERR(ERR_NAPI_PARAM_TYPE_ERROR,   "NAPI 参数类型错误"),
        DEFINE_ERR(ERR_NAPI_PARAM_NUMBER_WRONG, "NAPI参数个数错误"),
        DEFINE_ERR(ERR_NAPI_PARAM_INVALID,      "NAPI参数无效"),
        DEFINE_ERR(ERR_CONFIG_LOAD_FAILED,      "配置文件加载失败"),
        DEFINE_ERR(ERR_CONFIG_NOT_FIND,         "配置信息没找到")
    };

    void GetStatus(int id, char err[]) {
        if (g_sErr.find(id) == g_sErr.end()) {
            return;
        }
        strncpy(err, g_sErr.at(id).c_str(), 256);
    }

    void AddStatusList(const std::map<int, const std::string>& status_list) {
        g_sErr.insert(status_list.begin(), status_list.end());
    }

}  // namespace Finance
}   // namespace SwanLink
